package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inStorage.OtherMaterielOverviewAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageOtherMaterielOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageOtherMaterielOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import static com.inossem.warehousestorage_inossem.utils.InStorageUtils.getInStorageOtherBean;


//其他入库物料列表
public class InStorageOtherMaterielOverviewActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView materielList;

    private OtherMaterielOverviewAdapter adapter;

    private String mMaterielCode;

    private List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> allData;

    private InStorageOtherBean otherBean;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_other_materiel_overview);
        initView(InStorageOtherMaterielOverviewActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(InStorageOtherMaterielOverviewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        materielList = (PullableListView) activity.findViewById(R.id.materielList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.materielListRefresh);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
    }

    private void initData(Activity activity) {
        title.setText("物料列表");
        ActivityUtils.initBackAndFunction(activity, "其他入库", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        materielList.setPullUpFlag(false);
        materielList.setPullDownFlag(false);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check(activity)) {
                    Intent intent = new Intent(activity, InStorageOtherAddOperatorActivity.class);
                    startActivity(intent);
                }
            }
        });
        mMaterielCode = "";
        otherBean = getInStorageOtherBean(activity);
        allData = otherBean.getMaterielList() == null ? new ArrayList<>() : otherBean.getMaterielList();
        addMaterielListData(activity, allData, materielList, true);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity,mMaterielCode);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private boolean check(Context context) {
        int confirmNumber = InStorageUtils.getOtherInStorageConfirmNumber(context);
        if (confirmNumber == 0) {
            Utils.middleToast(context, "请至少完善一个行项目");
            return false;
        }
//        List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> list = InStorageUtils.getInStorageOtherBean(context).getMaterielList();
//        if (list == null || list.isEmpty()) {
//            Utils.middleToast(context, "至少添加一个物料");
//            return false;
//        }
//
//        int batchIndex = InStorageUtils.checkOtherBatchInfo(context);
//        if (batchIndex != -1) {
//            Utils.middleToast(context, "第" + batchIndex + "个行项目批次信息没填完");
//            return false;
//        }
//        int inStorageNumberIndex = InStorageUtils.checkOtherInStorageNumber(context);
//        if (inStorageNumberIndex != -1) {
//            Utils.middleToast(context, "第" + inStorageNumberIndex + "个行项目入库数量没填完");
//            return false;
//        }
        return true;
    }

    private void addMaterielListData(Context context, List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> materielList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new OtherMaterielOverviewAdapter(context, materielList, mMaterielCode, new InStorageOtherMaterielOverviewListener() {
                @Override
                public void deleteMaterielItem(String materielCode) {
                    DialogUtils.getConfirmDialog(context, "确认删除物料编码为" + materielCode + "的物料?", new DialogUtils.ConfirmDialogListener() {
                        @Override
                        public void onConfirm(Dialog dialog) {
                            dialog.cancel();
                            InStorageUtils.deleteOtherMateriel(context, materielCode);
                            otherBean = getInStorageOtherBean(context);
                            allData = otherBean.getMaterielList() == null ? new ArrayList<>() : otherBean.getMaterielList();
                            addMaterielListData(context, allData, InStorageOtherMaterielOverviewActivity.this.materielList, true);
                        }
                    }).show();
                }

                @Override
                public void onNoDoubleClickListener(String materielCode) {
                    if (TextUtils.isEmpty(materielCode)) {
                        Utils.middleToast(context, "物料编码不可以为空");
                    } else {
                        mMaterielCode = materielCode;
                        getData(context, mMaterielCode);
                    }
                }
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context, String materielCode) {
        InStorageOtherMaterielOverviewRequest.request(context, materielCode, new GsonBeanListener<InStorageOtherMaterielOverviewBean>() {
            @Override
            public void onSuccess(InStorageOtherMaterielOverviewBean bean) {
                List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> list = bean.getData().getMaterielList();
                if (list == null || list.isEmpty()) {
                    Utils.middleToast(context, "未查到新数据");
                } else {
                    allData = addMateriel(allData, list);
                    otherBean.setMaterielList(allData);
                    Gson gson = new Gson();
                    String json = gson.toJson(otherBean, InStorageOtherBean.class);
                    SPUtils.saveOtherInStorageInformation(context, json);
                    addMaterielListData(context, allData, materielList, true);
                }
            }
        });
    }

    private List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> addMateriel(List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> currentList, List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> addList) {
        if (currentList == null || currentList.isEmpty()) {
            return addList;
        }
        List<InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean> removeList = new ArrayList<>();
        for (int i = 0; i < addList.size(); i++) {
            InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean addItem = addList.get(i);
            for (int j = 0; j < currentList.size(); j++) {
                InStorageOtherMaterielOverviewBean.DataBean.MaterielListBean currentItem = currentList.get(j);
                if (addItem.getMaterielCode().equals(currentItem.getMaterielCode())) {
                    removeList.add(addItem);
                }
            }
        }
        addList.removeAll(removeList);
        currentList.addAll(0, addList);
        return currentList;
    }

    public interface InStorageOtherMaterielOverviewListener {
        void onNoDoubleClickListener(String supplierCode);

        void deleteMaterielItem(String materielCode);
    }

}
